<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>fech的post和put的请求方式</title>
</head>

<body>

</body>
<script>
  "use strict";
  fetch("http://localhost:3000/books", {
    method: "post",
    body: `uname=li&pwd=123`,
    headers: {
      "Content-Type": "application/x-www-form-urlencoded"
    }
  }).then(data => {
    return data.next();
  }).then(res => {
    console.log(res);
  });

  // 可以将其设置为json的对象
  fetch("http://localhost:3000/books", {
    method: "post",
    body: JSON.stringify({
      uname: "li",
      pwd: "123"
    }),
    headers: {
      "Content-Type": "application/json"
    }
  }).then(data => {
    return data.next();
  }).then(res => {
    console.log(res);
  });

  // 对应的后台接口
  app.post("/books", (req, res) => {
    res.send(`post请求传递的参数${req.body.uname} ${req.body.pwd}`);
  });
</script>
<script>
  "use strict";
  // PUT请求方式的参数的传递
  fetch("http://localhost:3000/books/123", {
    method: "put",
    body: JSON.stringify({
      uname: "li",
      pwd: "123"
    }),
    headers: {
      "Content-Type": "application/json"
    }
  }).then(data => {
    return data.next();
  }).then(res => {
    console.log(res);
  });

  // 对应的后台接口
  app.post("/books/:id", (req, res) => {
    res.send(`post请求传递的参数${req.params.id} ${req.body.uname} ${req.body.pwd}`);
  });
</script>

</html>